package com.laker.autotest.desktop.core;

import lombok.extern.slf4j.Slf4j;

import java.awt.event.KeyEvent;

/**
 * 桌面自动化测试执行入口
 */
@Slf4j
public class DesktopTestRunner {

    public static void main(String[] args) {
        try {
            log.info("开始执行桌面自动化用例...");
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                Runtime.getRuntime().exec("notepad");
                Thread.sleep(1000);
                // 激活 Notepad，确保输入焦点
                Runtime.getRuntime().exec("powershell -command \"(Get-Process notepad).MainWindowHandle | ForEach-Object { $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate($_) }\"");
                Thread.sleep(2000);
            } else if (os.contains("mac")) {
                Runtime.getRuntime().exec("open -a TextEdit");
                Thread.sleep(1500);
                // 激活 TextEdit，确保输入焦点
                Runtime.getRuntime().exec(new String[]{"osascript", "-e", "tell application \"TextEdit\" to activate"});
                Thread.sleep(2000);
            } else {
                log.warn("请手动打开一个文本编辑器窗口");
                Thread.sleep(2000);
            }
            // 可选：点击屏幕中央，确保输入区获得焦点
            DesktopRobotUtil.moveMouse(600, 400); // 可根据实际屏幕分辨率调整
            DesktopRobotUtil.mouseClick();
            Thread.sleep(500);
            // 输入内容
            log.info("准备输入：自动化测试演示");
            DesktopRobotUtil.typeText("自动化测试演示");
            DesktopRobotUtil.keyPress(KeyEvent.VK_ENTER);
            DesktopRobotUtil.keyRelease(KeyEvent.VK_ENTER);
            log.info("准备输入：Hello Desktop!");
            DesktopRobotUtil.typeText("Hello Desktop!");
            // 截图
            String screenshotPath = "desktop_test_screenshot.png";
            DesktopRobotUtil.captureScreen(screenshotPath);
            log.info("截图已保存: " + screenshotPath);

            log.info("【自动化测试演示】开始：");
            // 图像识别驱动自动化示例
            String inputBoxImage = "input_box.png"; // 请将目标输入框截图放在项目根目录
            boolean result = DesktopRobotUtil.findAndClick(inputBoxImage);
            if (result) {
                log.info("已定位并点击输入框，准备输入内容...");
                Thread.sleep(800); // 点击后延时，确保焦点切换
                DesktopRobotUtil.typeText("自动化测试演示");
                DesktopRobotUtil.keyPress(KeyEvent.VK_ENTER);
                DesktopRobotUtil.keyRelease(KeyEvent.VK_ENTER);
                DesktopRobotUtil.typeText("Hello Desktop!");
                log.info("输入内容已完成。");
            } else {
                log.warn("未找到目标图片，操作失败！");
            }
            // 再次截图
            String screenshotPath2 = "desktop_test_screenshot_after.png";
            DesktopRobotUtil.captureScreen(screenshotPath2);
            log.info("操作后截图已保存: " + screenshotPath2);
            log.info("【自动化测试演示】结束。");
            log.info("桌面自动化用例执行完成。");
        } catch (Exception e) {
            log.error("桌面自动化测试执行失败: ", e);
        } finally {
            // 结束所有打开的 Notepad/TextEdit 进程
            try {
                if (System.getProperty("os.name").toLowerCase().contains("win")) {
                    Runtime.getRuntime().exec("taskkill /F /IM notepad.exe");
                } else {
                    Runtime.getRuntime().exec("pkill TextEdit");
                }
            } catch (Exception e) {
                log.error("关闭文本编辑器失败: ", e);
            }
        }
    }
}
